﻿define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    
    require('../su/accordion/oaTabCtrl');
    
    var uService = require('core/services/biz/noticeService');
    var appsTypeConstants = uService.appsTypeConstants;
    var getTimeToString = uService.getTimeToString;
    var notice_hrefType = uService.notice_hrefType;
    require('../notice/notiCurtainCtrl');
    $.widget('rk.notiPanelCtrl', $.rk.oaPanelCtrl, {
            _language:"zh",
            _noticeCounts:"",
            _noticeCountsNum:"",
            //create方法中用来生成UI  create 中生成页面
            _create: function(){
                var me = this;
                var elem = me.element;
                if(navigator.userLanguage){
                    me._language = navigator.userLanguage.substring(0,2).toLowerCase();
                }else{
                    me._language = navigator.language.substring(0,2).toLowerCase();
                }
                var html;
                var temp = require('oa/tmpl/notice/tpl_notice_panel.tpl');
                html = rk.templateText(temp,{SESSION:SESSION});
                elem.html(html);
                //初始化手风琴
                me._initAccordion();
                me._renewCounts();
                $.registLongPulling(function(msg) {
                    if(msg.k == "newnotice" || msg.k == "remind" || msg.k == "at" || msg.k == "announcement" || msg.k == "notice") {
                        me._renewCounts();
                    }
                });
                me._listeners();
            },
            //init中绑定事件  事件的方法都写到组件下面
            _init: function(){
                var me = this;
                var elem = me.element;
                //以delegate方式给列表绑定点击事件
                elem.on('click', '[act="showAll"]', function (){
                    var list = $(this);
                    me._showSearch(list);
                });
            },
            _listeners:function(){
                var me = this;
                var elem = me.element;
                me._detailListener();
                me._all_readListener();
                me._announcementQueryListener();
                me._remind_contentListener();
                me._atme_contentListener();
                elem.on('click','.oa-close-panel',function(){
                    rk.oa.closePanel();
                });
            },
            _remind_contentListener:function(){
                var me = this;
                var elem = me.element;
                elem.on("click", "[act='notice_panel_row']", function(e){
                    var target = $(e.target);
                    var targetHref = target.attr("href");
                    if(target.is("img") || (targetHref && targetHref.indexOf("javascript:") == -1)){
                        return ;
                    }
                    var row = $(this);
                    var hidden = row.find("[act='hrefType']");
                    if(!hidden || hidden.length == 0){
                        hidden = row.find("[act='hreftype']");
                    }
                    var hrefType = hidden.attr("hrefType");
                    if(!hrefType || hrefType.length == 0){
                        hrefType = hidden.attr("hreftype");
                    }
                    if(notice_hrefType[hrefType]){
                        me.popupContent({
                            size:notice_hrefType[hrefType](hidden),
                            onSuccess: function (contentElem) {
                                notice_hrefType[hrefType](hidden,contentElem);
                                elem. find( "[act='notice_panel_row']"). removeClass( "show-content-hover");
                                row. addClass( "show-content-hover" );
                            }
                        });
                        var table = row.parent();
                        if(!row.attr("isRead") && table.attr("table_name") == "remind" && me._noticeCountsNum){
                            row.attr("isRead","true");
                            me._noticeCountsNum.remindUnreadCount = me._noticeCountsNum.remindUnreadCount - 1;
                            if(me._noticeCountsNum.remindUnreadCount < 0){
                                me._noticeCountsNum.remindUnreadCount = 0;
                            }
                            me._noticeCounts = {
                                remindUnreadCount:me._noticeCountsNum.remindUnreadCount,
                                noticeUnreadCount:me._noticeCountsNum.noticeUnreadCount,
                                atmeUnreadCount:me._noticeCountsNum.atmeUnreadCount,
                                annoUnreadCount:me._noticeCountsNum.annoUnreadCount
                            };
                            me._renewToCounts(me._noticeCounts);
                        }
                    }
                });
            },
            _atme_contentListener:function(){
                var me = this;
                var elem = me.element;
                elem.on("click", "[act='atme_row_div']", function(e){
                    var row = $(this);
                    var target = $(e.target);
                    var targetHref = target.attr("href");
                    if(target.is("img")){
                        return ;
                    }
                    if(!targetHref || targetHref.indexOf("javascript:") != -1) {
                        var showRowHover = function(){
                            elem. find( "[act='notice_panel_row']"). removeClass( "show-content-hover");
                            row. addClass( "show-content-hover" );
                        };
                        if (row.find("[act='atmeFeedShowType']") && row.find("[act='atmeFeedShowType']").length > 0) {
                            var hidden = row.find("[act='atmeFeedShowType']");
                            var feedId = hidden.attr("feedId");//跳转到动态详情页
                            me.popupContent({
                                //size: "middle",
                                onSuccess: function (contentElem) {
                                    require.async(["../colleague/colleagueContentCtrl"], function () {
                                        require.async(["core/apps/feed/feed"],function(){
                                            contentElem.addClass("oa-content-feed-detail");
                                            contentElem.feed2015({
                                                tabs: [{
                                                    tabname: 'none',
                                                    url: '/json/sns_feed/one.action?storyId='+feedId
                                                }],
                                                hasEditor: false
                                            });
                                            showRowHover();
                                        });
                                    });
                                }
                            });
                        } else if (row.find("[act='atmeCommentShowType']") && row.find("[act='atmeCommentShowType']").length > 0) {
                            var hidden = row.find("[act='atmeCommentShowType']");
                            var commentId = hidden.attr("commentId");
                            var systemId = hidden.attr("sid");
                            var systemItemId = hidden.attr("siid");
                            var uid = hidden.attr("uid");
                            if (systemId == 13) {
                                me.popupContent({
                                    size: "middle",
                                    onSuccess: function (contentElem) {
                                        require.async(["../colleague/colleagueContentCtrl"], function () {
                                            contentElem.colleagueContentCtrl({uid: systemItemId});
                                            showRowHover();
                                        });
                                    }
                                });
                            } else {
                                /*me.popupContent({
                                    size: notice_hrefType["detail"](hidden),
                                    onSuccess: function (contentElem) {
                                        notice_hrefType["detail"](hidden, contentElem);
                                    }
                                });*/
                                if(systemId == 521){//合同
                                    window.open("/final/contract.action?id=" + systemItemId);
                                }else if(systemId == 607 || systemId == 503){
                                    me.popupContent({
                                        //size: "middle",
                                        onSuccess: function (contentElem) {
                                            //notice_hrefType["detail"](hidden, contentElem);
                                            require.async(["core/apps/feed/feed"], function () {
                                                contentElem.addClass("oa-content-feed-detail");
                                                contentElem.feed2015({
                                                    tabs: [{
                                                        tabname: 'none',
                                                        url: '/json/sns_feed/one.action?systemId=' + systemId + '&itemId=' + systemItemId
                                                    }],
                                                    hasEditor: false
                                                });
                                                showRowHover();
                                            });
                                        }
                                    });
                                }else{
                                    me.popupContent({
                                        size: notice_hrefType["detail"](hidden),
                                        onSuccess: function (contentElem) {
                                            notice_hrefType["detail"](hidden, contentElem);
                                            showRowHover();
                                        }
                                    });
                                }
                            }
                        }
                        if(target.attr("act") == "notice_atme_comment_reply"){
                            var intervalTime = 0;
                            var id;
                            var focusText = function(){
                                intervalTime++;
                                var content = rk.oa.getContent();
                                var button = content.find(".js-feed-comment");
                                if(button && button.length > 0){
                                    window.clearInterval(id);
                                    button.trigger("click");
                                    var text = content.find(".js-comment-ta");
                                    if(text && text.length>0){
                                        var uname = target.attr("uname");
                                        text.focus();
                                        text.click();
                                        text.html("@"+uname+" ");
                                    }
                                }
                                var textea = content.find(".ui-autocomplete-input");
                                if(textea && textea.length > 0){
                                    window.clearInterval(id);
                                    var uname = target.attr("uname");
                                    textea.focus();
                                    textea.click();
                                    textea.html("@"+uname+" ");
                                }
                                if(intervalTime == 5){
                                    window.clearInterval(id);
                                }
                            };
                            id = window.setInterval(focusText,500);
                        }
                    }
                });
            },
            _all_readListener:function(){
                var me = this;
                var elem = me.element;
                elem.on('click', "[act='remind_all_read']", function(){
                    var button = $(this);
                    if(button.hasClass('disable')) return;
                    button.addClass('disable');
                    rk.post("/json/oa_notice/readAll-remind.action",{},function(json){
                        if(json.status===0){
                            me.loaddata("remind");
                            me._renewCounts();
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                       button.removeClass('disable');
                    });
                });
                elem.on("click", "[act='announcement_all_read']", function(){
                    var button = $(this);
                    if(button.hasClass('disable')) return;
                    button.addClass('disable');
                    rk.post("/json/oa_announcement/announcementAllReaded.action",{},function(json){
                        if(json.status===0){
                            me.loaddata("announcement");
                            me._renewCounts();
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                        button.removeClass('disable');
                    });
                });
            },
            _detailListener:function(){
                var me = this;
                var elem = me.element;
                elem.on('click', "[act='annoRow']", function(){
                    var row = $(this);
                    me.popupContent({
                        size:"middle",
                        defaultScroll: false,
                        onSuccess: function(contentElem){
                            //contentElem.notiContentCtrl({id:row.attr("aid"),type:"announcement"});
                            require.async(["../announcement/announcementContentCtrl.js"],function(c){
                                contentElem.announcementContentCtrl({id: row.attr("aid"), type: "announcement",come:"notice"});
                                elem. find( "[act='annoRow']"). removeClass( "show-content-hover");
                                row. addClass( "show-content-hover" );
                            });
                            if(!row.attr("isRead") && row.find(".new").length > 0) {
                                var table = row.parent();
                                row.attr("isRead", "true");
                                if (table.attr("table_name") == "announcement" && me._noticeCountsNum) {
                                    me._noticeCountsNum.annoUnreadCount = me._noticeCountsNum.annoUnreadCount - 1;
                                    me._noticeCounts = {
                                        remindUnreadCount: me._noticeCountsNum.remindUnreadCount,
                                        noticeUnreadCount: me._noticeCountsNum.noticeUnreadCount,
                                        atmeUnreadCount: me._noticeCountsNum.atmeUnreadCount,
                                        annoUnreadCount: me._noticeCountsNum.annoUnreadCount
                                    };
                                    me._renewToCounts(me._noticeCounts);
                                }
                            }
                        }
                    });
                });
            },
            _announcementQueryListener:function(){
                var me = this;
                var elem = me.element;
                //初始化  部门公告查询列表
                var ann_condition = elem.find('[act="announcement_condition"]');
                ann_condition.show();
                ann_condition.find('[act="ann_condition_dep"]').selectmenu();
                ann_condition.find('[act="ann_condition_type"]').selectmenu();
                ann_condition.find('[act="ann_condition_dep"]').selectmenu({
                    change:function(){
                        $.proxy(me._ann_condition_type_depQuery(),me);
                    }
                });
                ann_condition.find('[act="ann_condition_type"]').selectmenu({
                    change:function(){
                        $.proxy(me._ann_condition_type_depQuery(),me);
                    }
                });
                elem.on('click', "[act='ann_search_btn']", function(){
                    elem.find('[act="ann_announce-search-box"]').animate({left:"100%",right:"-100%"},0,null,function(){
                        elem.find('[act="ann_announce-search-box"]').show();
                        elem.find('[act="ann_announce-search-box"]').animate({left:0,right:0},300,null,function(){
                            elem.find("[act='ann_condition_title_name']").focus();
                        });
                    });
                });
                //这样写法，为了后面可以移除事件
                elem.find("[act='ann_condition_title_name']").bind("blur",function(e){
                    elem.find('[act="ann_announce-search-box"]').animate({left:"100%",right:"-100%"},300,null,function(){
                        elem.find('[act="ann_announce-search-box"]').hide();
                    })
                });
                var title_search=function(){
                    var titleElem = elem.find("[act='ann_condition_title_name']");
                    var title = titleElem.val();
                    var param = {pageNo:1,pageSize:20};
                    if(title){
                        param.title = title;
                        me.loaddata("announcement_titleQuery",param);
                    }
                };
                elem.on('mousedown', '[act="ann_title_name_search"]', function(e){
                    e.preventDefault();
                    title_search();
                });
                elem.on('keydown', '[act="ann_condition_title_name"]', function(e){
                    if(e.keyCode==13){
                        title_search();
                    }
                });
                var clear_ann_title_name = function(){
                    elem.find("[act='ann_condition_title_name']").val("");
                    elem.find("[act='ann_title_text_clear']").hide();
                };
                elem.on('click', '[act="ann_search_return_table"]', function(){
                    me.loaddata("announcement");
                    me._announcement_refresh();
                    clear_ann_title_name();
                });
                elem.on('mousedown', "[act='ann_title_text_clear']", function(e){
                    e.preventDefault();
                    clear_ann_title_name();
                });
                elem.on('input propertychange', "[act='ann_condition_title_name']", function(){
                    var val = elem.find("[act='ann_condition_title_name']").val();
                    if(val){
                        elem.find("[act='ann_title_text_clear']").show();
                    }else{
                        elem.find("[act='ann_title_text_clear']").hide();
                    }
                });
            },
            _announcement_refresh:function(){
                var me = this;
                var elem = me.element;
                var ann_condition = elem.find('[act="announcement_condition"]');
                elem.find('[act="ann_announce-title"]').show();
                elem.find('[act="ann_announce-search-box"]').hide();
                ann_condition.find('[act="ann_condition_dep"]').val("");
                ann_condition.find('[act="ann_condition_type"]').val("");
                ann_condition.find('[act="ann_condition_dep"]').selectmenu('refresh', true);
                ann_condition.find('[act="ann_condition_type"]').selectmenu('refresh', true);
                //重新绑定事件
                elem.find("[act='ann_condition_title_name']").bind("blur",function(){
                    //elem.find('[act="ann_announce-title"]').animate({left:"-100%"},0);
                    //elem.find('[act="ann_announce-title"]').animate({left:0},"slow");
                    elem.find('[act="ann_announce-search-box"]').animate({left:"100%",right:"-100%"},300,null,function(){
                        elem.find('[act="ann_announce-search-box"]').hide();
                    });
                });
            },
            _ann_condition_type_depQuery:function(){
                var me = this;
                var elem = me.element;
                var param={pageNo:1,pageSize:20};
                var ann_condition = elem.find('[act="announcement_condition"]');
                var groupId = ann_condition.find('[act="ann_condition_dep"]').val();
                var typeId = ann_condition.find('[act="ann_condition_type"]').val();
                if(groupId){
                    param.queryGroupId = groupId;
                }
                if(typeId){
                    param.queryTypeId = typeId;
                }
                me.loaddata("announcement",param);
            },
            _announcement_condition_load:function(){
                var me = this;
                var elem = me.element;
                var ann_condition = elem.find('[act="announcement_condition"]');
                var ann_dep_isload = elem.find("[act='ann_condition_dep']").attr("isload") == "false";
                var ann_type_isload = elem.find("[act='ann_condition_type']").attr("isload") == "false";
                if(ann_dep_isload && ann_type_isload) {
                    //加载公告部门
                    rk.post("/json/oa_announcement/initGroup.action", {pageNo: 1, pageSize: 20}, function (json) {
                        if (json.status === 0) {
                            var dataList = json.groupList;
                            var html = "<option value=''>"+rk.i18n('ANNOUNCEMENT_ALL_DEPART')+"</option>";
                            $.each(dataList, function (i, dataJson) {
                                html += "<option value='" + dataJson.id + "'>" + dataJson.name + "</option>";
                            });
                            elem.find("[act='ann_condition_dep']").html(html);
                            elem.find("[act='ann_condition_dep']").attr("isload","true");
                            ann_condition.find('[act="ann_condition_dep"]').selectmenu('refresh', true);
                            dataList = json.typeList;
                            if(dataList && dataList.length > 0) {
                                var html = "<option value=''>"+rk.i18n('ANNOUNCEMENT_ALL_TYPE')+"</option>";
                                $.each(dataList, function (i, dataJson) {
                                    html += "<option value='" + dataJson.id + "'>" + dataJson.name + "</option>";
                                });
                                elem.find("[act='ann_condition_type']").html(html);
                                elem.find("[act='ann_condition_type']").attr("isload", "true");
                                ann_condition.find('[act="ann_condition_type"]').selectmenu('refresh', true);
                            }else{
                                elem.find("[act='ann_condition_type']").hide();
                            }
                        } else {
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                    });
                }
            },
            loaddata:function(type,param,divs){
                var me = this;
                var elem = me.element;
                //加载提醒
                if(type==="remind"){
                    elem.find('[act="no-remind"]').hide();
                    elem.find('[table_name="' + type + '"]').beLoading();
                    rk.post("/json/oa_notice/unRead-remind-data.action",{},function(json){
                        if(json.status===0) {
                            var dataList=json.remindList;
                            var listHtml = "";
                            if(dataList && dataList.length>0) {
                                var tempArray = [];
                                $.each(dataList,function(i,dataJson){
                                    if(me.appsTypeConstants[dataJson.notice.appType]) {
                                        var tempName = me.appsTypeConstants[dataJson.notice.appType].tpl;
                                        tempArray[i] = "oa/tmpl/notice/template/tpl_" + tempName + ".tpl";
                                    }
                                });
                                var showLists = function(type,listHtml,elem) {
                                    elem.find('[table_name="' + type + '"]').html(listHtml);
                                    var remindHtml = '<a href="javascript:;" class="notice-list-more" act="showAll" forTable="remind">'+rk.i18n('NOTICE_HISTORY_SHOW')+'</a>';
                                    if (elem.find('[table_name="' + type + '"]').find("a[act='showAll']").length == 0) {
                                        elem.find('[table_name="' + type + '"]').append(remindHtml);
                                    }
                                };
                                require.async(tempArray, function() {
                                    var textArray = $.makeArray(arguments);//根据传的模版名，获取到返回的模版内容
                                    $.each(textArray,function(i,text){
                                        var tempHtml = rk.templateText(text,dataList[i]);
                                        listHtml += me.getNoticeRowHtml(json.nowTime, dataList[i], tempHtml, dataList[i].user);
                                    });
                                    showLists(type,listHtml,elem);
                                });
                                elem.find('[act="no-remind"]').hide();
                                elem.find("[act='remind_all_read']").show();
                            }else{
                                elem.find('[table_name="' + type + '"]').hide();
                                elem.find('[act="no-remind"]').show();
                                elem.find("[act='remind_all_read']").hide();
                            }
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                        elem.find('[act="beScroll"]').beScroll();
                    });
                }
                if(type==="unRead_notice"){
                    divs.beLoading();
                    //加载系统公告
                    rk.post("/json/oa_notice/unRead-notice-data.action",{},function(json){
                        if(json.status===0) {
                            var dataList=json.noticeList;
                            if(dataList && dataList.length>0) {
                                var listHtml = '';
                                var tempArray = [];
                                $.each(dataList,function(i,dataJson){
                                    if(me.appsTypeConstants[dataJson.notice.appType]) {
                                        var tempName = me.appsTypeConstants[dataJson.notice.appType].tpl;
                                        tempArray[i] = "oa/tmpl/notice/template/tpl_" + tempName + ".tpl";
                                    }
                                });
                                var showLists = function(type,listHtml,divs) {
                                    var noticeHtml = '<a href="javascript:;" class="notice-list-more" act="showAll" forTable="notice">'+rk.i18n('NOTICE_HISTORY_SHOW')+'</a>';
                                    divs.html(listHtml + noticeHtml);
                                    var notice_panel_rows = elem.find("[act='notice_panel_row']");
                                    for(var i = 0 ; i<notice_panel_rows.length ; i++){
                                        var row = $(notice_panel_rows[i]);
                                        var hidden = row.find("[hrefType='no_click']");
                                        var hiddenDel = row.find("[hrefType='deleteHref']");
                                        if(hidden && hidden.length >0){
                                            row.css("cursor","default");
                                        }
                                        if(hiddenDel && hiddenDel.length >0){
                                            row.css("cursor","default");
                                        }
                                    }
                                };
                                require.async(tempArray, function() {
                                    var textArray = $.makeArray(arguments);//根据传的模版名，获取到返回的模版内容
                                    $.each(textArray,function(i,text){
                                        var tempHtml = rk.templateText(text,dataList[i]);
                                        listHtml += me.getNoticeRowHtml(json.nowTime, dataList[i], tempHtml, dataList[i].user);
                                    });
                                    var showListHtml = '<div class="oa-list report-list-area daily-paper-list submit-me-approval  clear">'+listHtml+"</div>";
                                    showLists(type,showListHtml,divs);
                                    //elem.find("[act='notice_unRead_count']").val("");
                                    //elem.find("[act='notice_unRead_count']").hide();
                                    me._noticeCounts.noticeUnreadCount = 0;
                                });
                            }else{
                                var temp = require('oa/tmpl/notice/tpl_notice_none_panel.tpl');
                                var html = rk.templateText(temp,{text:rk.i18n('NOTICE_NEW_ALL_READ'),table_name:"notice"});
                                divs.html(html);
                            }
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                    });
                }
                if(type==="unRead_atme"){
                    divs.beLoading();
                    //加载@我的
                    rk.post("/json/oa_at/unReadAts.action",{},function(json){
                        if(json.status===0) {
                            var dataList = json.body.atList;
                            if (dataList && dataList.length>0) {
                                var listHtml = "";
                                $.each(dataList,function(i,dataJson){
                                    var tempHtml = "";
                                    var user;
                                    if (dataJson.type == 1 && dataJson.comment) {
                                        dataJson.comment.content = me._contentToUserAt(dataJson.comment.content, dataJson.comment.ats);
                                        var temp = require('oa/tmpl/notice/template/tpl_atme_comment_table_row.tpl');
                                        tempHtml = rk.templateText(temp,dataJson.comment);
                                        user = dataJson.comment.user;
                                    } else if (dataJson.type == 0 && dataJson.feed) {
                                        dataJson.feed.content = me._contentToUserAt(dataJson.feed.content, dataJson.feed.ats);
                                        var temp = require('oa/tmpl/notice/template/tpl_atme_feed_table_row.tpl');
                                        tempHtml = rk.templateText(temp,dataJson.feed);
                                        user = dataJson.feed.user;
                                    }
                                    listHtml += me.getAtMeRowHtml(json.nowTime, dataJson, tempHtml, user);
                                });
                                var atmeHtml = '<a href="javascript:;" class="notice-list-more" act="showAll" forTable="atme">'+rk.i18n('NOTICE_HISTORY_SHOW')+'</a>';
                                var showListHtml =  '<div class="oa-list report-list-area daily-paper-list notice-list-area  clear">'+listHtml+"</div>";
                                divs.html(showListHtml + atmeHtml);
                                //elem.find("[act='atme_unRead_count']").val("");
                                //elem.find("[act='atme_unRead_count']").hide();
                                me._noticeCounts.atmeUnreadCount = 0;
                            }else{
                                var temp = require('oa/tmpl/notice/tpl_notice_none_panel.tpl');
                                var html = rk.templateText(temp,{text:rk.i18n('NOTICE_NEW_ALL_READ'),table_name:"atme"});
                                divs.html(html);
                            }
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                    });
                }
                //加载部门公告
                if(type === "announcement"){
                    elem.find('[table_name="announcement"]').beLoading();
                    elem.find('[act="announcement_page"]').hide();
                    elem.find("[act='announcement_page_parent']").hide();
                    if(!param){
                        param = {pageNo:1,pageSize:20};
                    }
                    me._announcement_condition_load();
                    rk.post("/json/oa_announcement/all.action",param,function(json){
                        if(json.status===0) {
                            var dataList = json.body.announcements;
                            if(dataList && dataList.length>0) {
                                var listHtml = "";
                                var hasUnread = false;
                                $.each(dataList, function (i, dataJson) {
                                    listHtml += me.getAnnouncementRowHtml(json.nowTime, dataJson);
                                    if(!hasUnread && !dataJson.isRead){
                                        hasUnread = true;
                                    }
                                });
                                elem.find('[table_name="announcement"]').html(listHtml);
                                elem.find('[act="announcement_page"]').show();
                                if(hasUnread) {
                                    elem.find("[act='announcement_all_read']").show();
                                }else{
                                    elem.find("[act='announcement_all_read']").hide();
                                }
                            }else{
                                var temp = require('oa/tmpl/notice/tpl_notice_announcement_none_panel.tpl');
                                var html = rk.templateText(temp,{text:rk.i18n('ANNOUNCEMENT_NO_DEPART')});
                                elem.find('[table_name="announcement"]').html(html);
                                elem.find('[act="announcement_page"]').hide();
                                elem.find("[act='announcement_all_read']").hide();
                            }
                            elem.find('[act="ann_search_operation"]').hide();
                            elem.find("[act='announcement_page_parent']").show();
                            elem.find('[act="announcement_page"]').rkPaging({
                                pagenum: json.body.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.body.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.body.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.body.pageSize;
                                    me.loaddata(type,params,divs);
                                }
                            });
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                        elem.find('[act="beScroll"]').beScroll();
                    });
                }
                if(type === "announcement_titleQuery"){
                    elem.find('[table_name="announcement"]').beLoading();
                    elem.find('[act="announcement_page"]').hide();
                    if(!param){
                        param = {pageNo:1,pageSize:20};
                    }
                    elem.find("[act='announcement_page_parent']").hide();
                    rk.post("/json/oa_announcement/all.action",param,function(json){
                        if(json.status===0) {
                            var dataList = json.body.announcements;
                            elem.find("[act='ann_search_number']").html(json.body.dataCount);
                            if(dataList && dataList.length>0) {
                                var listHtml = "";
                                $.each(dataList, function (i, dataJson) {
                                    listHtml += me.getAnnouncementRowHtml(json.nowTime, dataJson);
                                });
                                elem.find('[table_name="announcement"]').html(listHtml);
                                elem.find('[act="announcement_page"]').show();
                            }else{
                                var temp = require('oa/tmpl/notice/tpl_notice_announcement_none_panel.tpl');
                                var html = rk.templateText(temp,{text:rk.i18n('ANNOUNCEMENT_NO_DEPART')});
                                elem.find('[table_name="announcement"]').html(html);
                                elem.find('[act="announcement_page"]').hide();
                            }
                            elem.find('[act="ann_search_operation"]').show();
                            elem.find("[act='ann_condition_title_name']").unbind("blur");//移除焦点事件
                            elem.find("[act='announcement_page_parent']").show();
                            elem.find('[act="announcement_page"]').rkPaging({
                                pagenum: json.body.pageNo,  //（可选）当前页数，默认1
                                pagesize: json.body.pageSize,  //（可选）当前每页显示条数，默认20
                                rowscount: json.body.dataCount, //所有数据数
                                onChange: function (pagenum) {
                                    var params = param;
                                    params.pageNo = pagenum;
                                    params.pageSize = json.body.pageSize;
                                    me.loaddata(type,params,divs)
                                }
                            });
                        }else{
                            rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                        }
                        elem.find('[act="beScroll"]').beScroll();
                    });
                }
            },
            //初始化手风琴
            _initAccordion:function(){
                var me = this;
                var elem = me.element;
                var opt = me.options;
                var active = false;
                if(opt.defaultShow){
                    active = opt.defaultShow.accordion ? opt.defaultShow.accordion :false;
                }
                //手风琴
                me.getPanelContent().find('> .wr-accordion').rkAccordion({
                    active:active,
                    heightStyle: "fill",
                    event:"click",
                    beforeActivate:function(event,ui){
                        if(ui.newPanel.context && ui.newPanel.context.nextElementSibling){
                            var h3=$(ui.newPanel.context);
                            var panel=$(ui.newPanel.context.nextElementSibling);
                            var type = h3.attr("forAct");
                            if(type != "notice"){
                                me.loaddata(type);
                            }
                            //me._renewCounts();
                            if(type == "announcement") {
                                me._announcement_refresh();
                            }
                            me._renewToCounts(me._noticeCounts);
                        }
                    },
                    onOpen: function(e, ui/*index, panel*/){
                        me._onAccordionOpen(e, ui);
                    }
                });
            },
            _onAccordionOpen:function(e, ui/*index, panel*/){
                var me = this;
                var elem = me.element;
                var opt = me.options;

                var subPanel = ui.panel;
                var panelIndex = ui.index;
                    if(subPanel.attr("act")!="notice"){
                        return;
                    }
                    //初始化标签页
                    if(subPanel.find("[forTab-name='unRead_notice']")){
                        subPanel.find("[forTab-name='unRead_notice']").attr("init","true");
                    }
                    if(subPanel.find("[forTab-name='unRead_atme']")){
                        subPanel.find("[forTab-name='unRead_atme']").attr("init","true");
                    }
                    subPanel.oaTabCtrl({
                        tabs: [{
                            title: rk.i18n('NOTICE_SYSTEM_NOTICE') +'<span class="hidden" act="notice_unRead_count"></span>',          //按钮标题
                            html_attrs: {           //按钮的html属性
                                "forTab-name":"unRead_notice",
                                "init":"true"
                            }
                        },{
                            title: rk.i18n("NOTICE_ATME") + '<span class="hidden" act="atme_unRead_count"></span>',          //按钮标题
                            html_attrs: {           //按钮的html属性
                                "forTab-name":"unRead_atme",
                                "init":"true"
                            }
                        }],
                        onTabSelect: function(e, info){//初始化标签页
                            var thisBtn = info.tabBtn;
                            var tab = info.child;
                            var forTab = thisBtn.attr("forTab-name");
                            var isInit = thisBtn.attr("init");
                            if(isInit == "true"){
                                me.loaddata(forTab,"",tab);
                                thisBtn.attr("init","false");
                            }
                        }
                    });
                    me._renewToCounts(me._noticeCounts);
                    if(opt.defaultShow && opt.defaultShow.tabs){
                        subPanel.oaTabCtrl('select', opt.defaultShow.tabs);
                    }else {
                        subPanel.oaTabCtrl('select', 0);
                    }
            },
            //第二层
            _showSearch: function(thisElem){
                var me = this;
                var onSuccess = function(curtain){
                    curtain.notiCurtainCtrl({type:thisElem.attr("forTable")});
                };
                me.popupCurtain({onSuccess:onSuccess});
            },
            //刷新数量
            _renewCounts:function(){
                var me = this;
                var elem = me.element;
                rk.post("/json/oa_notice/unRead-counts.action?a="+Math.random(),{},function(json){
                    if(json.status===0){
                        me._noticeCounts = json.unRead_counts;
                        me._noticeCountsNum = {
                            remindUnreadCount:json.unRead_counts.remindUnreadCount,
                            noticeUnreadCount:json.unRead_counts.noticeUnreadCount,
                            atmeUnreadCount:json.unRead_counts.atmeUnreadCount,
                            annoUnreadCount:json.unRead_counts.annoUnreadCount
                        };
                        me._renewToCounts(json.unRead_counts);
                    }else{
                        rk.noticeError(rk.i18n('ERROR_SYSTEM'));
                    }
                });
            },
            _renewToCounts : function(data){
                var me = this;
                var elem = me.element;
                me.globalNotice = $('#globalNotice').pageNoticeBtnCtrl();
                var nums = data.remindUnreadCount + data.noticeUnreadCount + data.atmeUnreadCount + data.annoUnreadCount;
                if(data.remindUnreadCount == "99+" || data.noticeUnreadCount == "99+" || data.atmeUnreadCount == "99+" || data.annoUnreadCount == "99+" || nums>99){
                    nums = "99+";
                }
                me.globalNotice.pageNoticeBtnCtrl('setNum', nums);
                if(data.remindUnreadCount == "99+" || data.remindUnreadCount>0){
                    elem.find("[act='remind_unRead_count']").show();
                    data.remindUnreadCount = data.remindUnreadCount>99 || data.remindUnreadCount == "99+" ? "99+" : data.remindUnreadCount;
                    elem.find("[act='remind_unRead_count']").html(data.remindUnreadCount);
                }else{
                    elem.find("[act='remind_unRead_count']").hide();
                }
                var notice_atme_count = data.noticeUnreadCount + data.atmeUnreadCount;
                if(data.noticeUnreadCount == "99+" || data.atmeUnreadCount == "99+" || notice_atme_count>0) {
                    elem.find("[act='notice_atme_unRead_count']").show();
                    notice_atme_count = notice_atme_count>99 || data.noticeUnreadCount == "99+" || data.atmeUnreadCount == "99+" ? "99+" :notice_atme_count;
                    elem.find("[act='notice_atme_unRead_count']").html(notice_atme_count);
                }else{
                    elem.find("[act='notice_atme_unRead_count']").hide();
                }

                if(data.annoUnreadCount == "99+" || data.annoUnreadCount>0) {
                    elem.find("[act='anno_unRead_count']").show();
                    data.annoUnreadCount = data.annoUnreadCount>99 || data.annoUnreadCount == "99+" ? "99+" : data.annoUnreadCount;
                    elem.find("[act='anno_unRead_count']").html(data.annoUnreadCount);
                }else{
                    elem.find("[act='anno_unRead_count']").hide();
                }
                var notice_unread = elem.find("[act='notice_unRead_count']");
                var atme_unread = elem.find("[act='atme_unRead_count']");
                if(notice_unread){
                    if(data.noticeUnreadCount == "99+" || data.noticeUnreadCount>0){
                        notice_unread.show();
                        data.noticeUnreadCount = data.noticeUnreadCount>99 || data.noticeUnreadCount == "99+" ? "99+" : data.noticeUnreadCount;
                        notice_unread.html("("+data.noticeUnreadCount+")");
                    }else{
                        notice_unread.hide();
                    }
                }
                if(atme_unread){
                    if(data.atmeUnreadCount == "99+" || data.atmeUnreadCount>0){
                        atme_unread.show();
                        data.atmeUnreadCount = data.atmeUnreadCount > 99 || data.atmeUnreadCount == "99+" ? "99+" :data.atmeUnreadCount;
                        atme_unread.html("("+data.atmeUnreadCount+")");
                    }else{
                        atme_unread.hide();
                    }
                }
            },
            _getTime:function(time){
                return time;
            },
            _contentToUserAt:function(content,ats){
                if(content && content.length > 100){
                    content = content.substr(0,100);
                    content+="...";
                }
                if(ats){
                    content+=" ";
                    $.each(ats,function(i,item){
                        content = content.replace("@"+item.name+" ","<a href='/final/user.action?uid="+item.uid+"' hrefType='user' target='_blank' ucard='uid="+item.uid+"' uid='"+item.uid+"'>@"+item.name+"</a> ");
                    });
                    content = content.substr(0,content.length-1);
                    return content;
                }else{
                    return content;
                }
            },
            getNoticeRowHtml:function(nowTime,dataJson,tempHtml,user){
                var me = this;
                if(user) {
                    if (!user.smallIcon) {
                        user.smallIcon = rk.DEFAULT_USER_ICON;
                    }
                }
                var temp = require('oa/tmpl/notice/template/tpl_NoticeRow_table.tpl');
                var html = rk.templateText(temp,{user:user,time:me.getTimeToString(dataJson.notice.createdAt,nowTime,me),tempHtml:tempHtml});
                return html;
            },
            getAtMeRowHtml:function(nowTime,dataJson,tempHtml,user){
                var me = this;
                var elem = me.element;
                var time;
                if(dataJson.feed){
                    time = dataJson.feed.date;
                }else if(dataJson.comment){
                    time = dataJson.comment.date;
                }
                if(user) {
                    if (!user.icon) {
                        user.smallIcon = rk.DEFAULT_USER_ICON;
                    }
                }
                var temp = require('oa/tmpl/notice/template/tpl_AtMeRow_table.tpl');
                var html = rk.templateText(temp,{user:user,time:me.getTimeToString(time,nowTime,me),tempHtml:tempHtml});
                return html;
            },
            getAnnouncementRowHtml:function(nowTime,dataJson){
                var me = this;
                var elem = me.element;
                if(dataJson.created){
                    dataJson.created = me.getTimeToString(dataJson.created,nowTime,me);
                }
                var temp = require('oa/tmpl/notice/template/tpl_noti_announcement_row.tpl');
                var html = rk.templateText(temp,dataJson);
                return html;
            },
            getTimeToString:getTimeToString,
            appsTypeConstants:appsTypeConstants
    });
});